// 小黄人面部表情控制
document.addEventListener('DOMContentLoaded', function() {
    const loginCard = document.querySelector('.login-card');
    const boi = document.querySelector('.Boi');
    const btnsTie = document.querySelectorAll('.boi-btn-tie');
    const btnBeat = document.querySelector('.boi-btn-beat');
    
    const current = {
        happiness: 0.9,
        derp: 1,
        px: 0.5,
        py: 0.5
    };
    
    const target = { ...current };
    
    // 登录卡片上的鼠标移动事件
    if (loginCard && boi) {
        loginCard.addEventListener('mousemove', onMouseMove);
        loginCard.addEventListener('mouseleave', onMouseLeave);
    }
    
    // 按钮事件监听
    if (btnsTie.length > 0) {
        btnsTie.forEach(btn => {
            btn.addEventListener('mouseover', function() {
                target.happiness = 1;
                target.derp = 0;
            });
            
            btn.addEventListener('mouseout', function() {
                target.happiness = 0.9;
                target.derp = 1;
            });
            
            btn.addEventListener('click', function() {
                showBoiAnimation('tie');
            });
        });
    }
    
    if (btnBeat) {
        btnBeat.addEventListener('mouseover', function() {
            target.happiness = 0;
            target.derp = 0.5;
        });
        
        btnBeat.addEventListener('mouseout', function() {
            target.happiness = 0.9;
            target.derp = 1;
        });
        
        btnBeat.addEventListener('click', function() {
            showBoiAnimation('beat');
        });
    }
    
    // 鼠标移动处理函数
    function onMouseMove({ clientX: x, clientY: y }) {
        if (!boi) return;
        
        // 计算鼠标相对于登录卡片的位置
        const rect = loginCard.getBoundingClientRect();
        let px = (x - rect.x) / rect.width;
        let py = (y - rect.y) / rect.height;
        
        // 按钮距离计算
        if (btnsTie.length > 0 || btnBeat) {
            let distTie = Infinity;
            let distBeat = Infinity;
            
            // 计算与贴贴按钮的最短距离
            btnsTie.forEach(btn => {
                const btnRect = btn.getBoundingClientRect();
                let dx = x - btnRect.x - btnRect.width * 0.5;
                let dy = y - btnRect.y - btnRect.height * 0.5;
                let dist = Math.sqrt(dx * dx + dy * dy);
                
                if (dist < distTie) {
                    distTie = dist;
                }
            });
            
            // 计算与暴揍按钮的距离
            if (btnBeat) {
                const btnRect = btnBeat.getBoundingClientRect();
                let dx = x - btnRect.x - btnRect.width * 0.5;
                let dy = y - btnRect.y - btnRect.height * 0.5;
                distBeat = Math.sqrt(dx * dx + dy * dy);
            }
            
            // 根据距离计算幸福度
            if (distTie < 100 || distBeat < 100) {
                let totalDist = distTie + distBeat;
                // 离贴贴近时开心，离暴揍近时不开心
                let happiness = Math.pow(distBeat / (totalDist || 1), 0.75);
                target.happiness = happiness;
                target.derp = 0;
            } else {
                target.happiness = 0.9;
                target.derp = 0.2;
            }
        }
        
        target.px = px;
        target.py = py;
    }
    
    // 鼠标离开处理函数
    function onMouseLeave() {
        target.happiness = 0.9;
        target.derp = 1;
        target.px = 0.5;
        target.py = 0.5;
    }
    
    // 显示特殊动画
    function showBoiAnimation(type) {
        if (type === 'tie') {
            // 贴贴动画
            boi.classList.add('boi-tie-animation');
            setTimeout(() => {
                boi.classList.remove('boi-tie-animation');
                
                // 添加CSS动画类触发表情变化
                const loginForm = document.querySelector('.login-form');
                if (loginForm) {
                    loginForm.classList.add('form-show');
                }
            }, 1000);
        } else if (type === 'beat') {
            // 暴揍动画
            boi.classList.add('boi-beat-animation');
            setTimeout(() => {
                boi.classList.remove('boi-beat-animation');
                
                // 隐藏小黄人并显示登录表单
                const loginForm = document.querySelector('.login-form');
                if (loginForm) {
                    loginForm.classList.add('form-show');
                    boi.style.opacity = '0';
                    setTimeout(() => {
                        boi.style.display = 'none';
                    }, 500);
                }
            }, 1000);
        }
    }
    
    // 更新表情动画
    function update() {
        if (!boi) return;
        
        for (let prop in target) {
            if (target[prop] === current[prop]) {
                continue;
            } else if (Math.abs(target[prop] - current[prop]) < 0.01) {
                current[prop] = target[prop];
            } else {
                current[prop] += (target[prop] - current[prop]) * 0.1;
            }
            boi.style.setProperty(`--${prop}`, current[prop]);
        }
        requestAnimationFrame(update);
    }
    
    // 启动动画
    update();
}); 